<template>
	<view>
		<view class="pokerBox">
			<block v-for="(item, index) in cardData" :key="index">
				<view :data-index="index" :data-item="item" @click="openCard"
					:class="'project item' + index + ' ' + (item.is_move ? 'ani' : '') + ' ' + (item.status==1 ? 'flip' : '')">
					<image class="front" src="../../static/image/pai.png"></image>
					<view class="back" :style="{opacity:(activeIndex !=-1 && activeIndex != index)?0.5:1}">
						<view class="z-flex z-flex-justify-c z-flex-align-c"
							style="position: absolute;top: 0;z-index: 1;width: 100%;height: 100%;box-sizing: border-box;flex-direction: column;text-align: center;padding-right: 10rpx;padding-top: 50rpx;">
							<text style="font-size: 32rpx;font-weight: bold;color: #ca635e;">
								{{item.val}}<text style="font-size: 26rpx;"></text>
							</text>
						</view>
						<image src="../../static/image/pai1.png"></image>
					</view>
				</view>
			</block>
		</view>
	</view>
</template>

<script>
	function runAsync(time) {
		return new Promise(function(resolve, reject) {
			const timer = setTimeout(function() {
				resolve();
				clearTimeout(timer);
			}, time);
		});
	}
	export default {
		name: "kapai",
		data() {
			return {
				cardData: this.card, //数据
				iscsh: true,
				clickDisable: true, //是否禁止点击
				stopRewardFlag: false, //是否停止抽奖
				activeIndex: -1,
			};
		},
		props: {
			// 九宫格卡片
			card: {
				type: Array,
				default: () => {
					return []
				}
			},
			extractNum: {
				// 每人总抽奖次数
				type: Number,
				default: 1
			}
		},
		watch: {
			card: {
				deep: true,
				handler(v) {
					this.cardData = v
				}
			},
			extractNum: {
				handler(val) {
					this.extractNum = val
				},
				immediate: true
			}
		},
		methods: {
			async start(cardData, callback) {
				// 开始洗牌
				this.iscsh = false
				this.clickDisable = true;
				this.stopRewardFlag = false;
				const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

				try {
					for (let i = 0; i < 3; i++) {
						cardData[i].status = 1;
					}
					this.cardData = cardData;
					await delay(200);

					for (let i = 3; i < 6; i++) {
						cardData[i].status = 1;
					}
					this.cardData = cardData;
					await delay(200);

					for (let i = 6; i <= 8; i++) {
						cardData[i].status = 1;
					}
					this.cardData = cardData;
					await delay(200);

					for (let i = 0; i < 9; i++) {
						cardData[i].status = 0;
					}
					this.cardData = cardData;
					await delay(800);

					for (let i = 0; i < 9; i++) {
						if (i != 4) {
							await delay(20);
							cardData[i].is_move = true;
							this.cardData = cardData;
							await delay(10);
							cardData[i].is_move = false;
							this.cardData = cardData;
							this.$forceUpdate();
							await delay(200);
							if (typeof callback === 'function' && i === 8) {
								await delay(1000);
								uni.showToast({
									title: '洗牌完成,请选择',
									icon: 'none',
								})
								callback();
							}
						} else {
							cardData[i].is_move = false;
						}

					}
					this.clickDisable = false;
				} catch (err) {
					this.clickDisable = false;
				}
			},
			// 选择卡片
			openCard(event) {
				if (this.extractNum <= 0) {
					uni.showToast({
						title: "抽牌已用完，谢谢",
						duration: 2000,
						icon: 'none'
					});
					return false;
				}
				if (this.iscsh) {
					uni.showToast({
						title: '请先洗牌',
						icon: 'none'
					})
					return false;
				}
				if (this.stopRewardFlag) {
					uni.showToast({
						title: '请重新洗牌',
						icon: 'none'
					})
					return false;
				}
				if (this.clickDisable) {
					uni.showToast({
						title: '正在洗牌中...',
						icon: 'none'
					})
					return false;
				}
				const {
					item,
					index
				} = event.currentTarget.dataset; // 触发父组件方法

				this.openReward(index, this.cardData)
				// 选择一张卡片后停止抽奖
				this.stopReward()
			},
			openReward(originalIndex, cardArrRes) {
				this.cardData = cardArrRes;
				this.activeIndex = originalIndex
				setTimeout(() => {
					this.cardData[originalIndex]['status'] = 1;
					let nowCardData = this.cardData[originalIndex];
					let cardDataArr = this.cardData;
					this.$emit('success', {
						detail: {
							item: nowCardData,
							index: originalIndex
						}
					});

				});
			},
			stopReward() {
				// 停止抽个奖
				this.stopRewardFlag = true;
			}
		}
	};
</script>
<style>
	.pokerBox {
		position: relative;
		overflow: hidden;
		margin: 0 auto;
		width: 600rpx;
		height: 780rpx
	}

	.pokerBox .project {
		position: absolute;
		float: left;
		width: 200rpx;
		height: 200rpx;
		display: -ms-flexbox;
		display: flex;
		-ms-flex-pack: center;
		justify-content: center;
		-ms-flex-align: center;
		align-items: center;
		transition: all 1s cubic-bezier(.68, -.22, .265, 1.22)
	}

	.pokerBox .project image {
		width: 180rpx;
		height: 200rpx
	}

	.pokerBox .project .back,
	.pokerBox .project .front {
		-webkit-backface-visibility: hidden;
		backface-visibility: hidden;
		transform-style: preserve-3d;
		transition: all 1s cubic-bezier(.68, -.22, .265, 1.22);
		position: absolute;
		left: 10rpx;
		top: 10rpx;
		width: 180rpx;
		height: 200rpx;
		margin-top: 20px
	}

	.pokerBox .project .front {
		transform: rotateY(0)
	}

	.pokerBox .project .back {
		transform: rotateY(180deg)
	}

	.pokerBox .project.flip .back {
		transform: rotateY(0);
		margin-left: 6px;
		
	}

	.pokerBox .project.flip .front {
		transform: rotateY(180deg)
	}

	.pokerBox .ani {
		/* 洗牌的时候统一 */
		top: 260rpx !important;
		left: 200rpx !important
	}

	.pokerBox .item0 {
		top: 0;
		left: 0
	}

	.pokerBox .item1 {
		top: 0;
		left: 200rpx
	}

	.pokerBox .item2 {
		top: 0;
		left: 400rpx
	}

	.pokerBox .item3 {
		top: 260rpx;
		left: 0
	}

	.pokerBox .item4 {
		top: 260rpx;
		left: 200rpx
	}

	.pokerBox .item5 {
		top: 260rpx;
		left: 400rpx
	}

	.pokerBox .item6 {
		top: 520rpx;
		left: 0
	}

	.pokerBox .item7 {
		top: 520rpx;
		left: 200rpx
	}

	.pokerBox .item8 {
		top: 520rpx;
		left: 400rpx
	}
</style>